//
//  DataBase.m
//  Test
//
//  Created by 喂！ on 2018/7/10.
//  Copyright © 2018年 喂！. All rights reserved.
//

#import "DataBase.h"

static DataBase *_DBCtl = nil;

@interface DataBase()
{
    FMDatabase  *_db;
}
@end
@implementation DataBase


+(instancetype)sharedDataBase
{
    if (!_DBCtl) {
        _DBCtl = [DataBase new];
        [_DBCtl initDataBase];
    }
    return _DBCtl;
}

+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    if (!_DBCtl) {
        _DBCtl = [super allocWithZone:zone];
    }
    return _DBCtl;
}

-(id)copy
{
    return self;
}
-(id)mutableCopy
{
    return self;
}

-(void)initDataBase
{
    //获得Documents目录路径
    NSString *documentsPth = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    //文件路径
    NSString *filePath = [documentsPth stringByAppendingPathComponent:@"model.db"];
    NSLog(@"数据库路径   :%@",filePath);
    
    //实例化FMDBDataBase对象
    
    _db = [FMDatabase databaseWithPath:filePath];
    [_db open];
    
    //初始化数据表
    NSString *personSql = @"create table 'person' ('id' integer primary key autoincrement not null ,'person_id' varchar(255),'person_name' varchar(255),'person_age' varchar(255),'person_number'varchar(255))";
    NSString *carSql = @"create table 'car' ('id' integer primary key autoincrement not null ,'own_id' varchar(255),'car_id' varchar(255),'car_brand' varchar(255),'car_price'VARCHAR(255))";
    [_db executeUpdate:personSql];
    [_db executeUpdate:carSql];
    
    [_db close];
}

#pragma mark    接口
-(void)addPerson:(Person *)person
{
    [_db open];
    NSNumber *maxID = @(0);
    FMResultSet *res = [_db executeQuery:@"select * from person"];
    //获取数据库中最大的ID
    while ([res next]) {
        if ([maxID integerValue] < [[res stringForColumn:@"person_id"] integerValue]) {
            maxID = @([[res stringForColumn:@"person_id"] integerValue]);
        }
    }
    
    maxID = @([maxID integerValue] + 1);
    [_db executeUpdate:@"insert into person(person_id,person_name,person_age,person_number)values(?,?,?,?)",maxID,person.name,@(person.age),@(person.number)];
    [_db close];
}

-(void)deletePerson:(Person *)person
{
    [_db open];
    [_db executeUpdate:@"delete from person where person_id = ?",person.ID];
    [_db close];
}

-(void)updatePerson:(Person *)person
{
    [_db open];
    
    [_db executeUpdate:@"update 'person' set person_name = ? where person_id = ?",person.name,person.ID];
    [_db executeUpdate:@"update 'person' set person_age = ? where person_id = ?",@(person.age),person.ID];
    [_db executeUpdate:@"update 'person' set person_number = ? where person_id = ?",@(person.number + 1),person.ID];
    
    [_db close];
}

-(NSMutableArray *)getAllPerson
{
    [_db open];
    NSMutableArray *dataArray = [NSMutableArray array];
    FMResultSet *res = [_db executeQuery:@"select * from person"];
    while ([res next]) {
        Person *person = [Person new];
        person.ID = @([[res stringForColumn:@"person_id"] integerValue]);
        person.name = [res stringForColumn:@"person_name"];
        person.age = [[res stringForColumn:@"person_age"] integerValue];
        person.number = [[res stringForColumn:@"person_number"] integerValue];
        [dataArray addObject:person];
    }
    [_db class];
    return dataArray;
}

//给person添加车辆
-(void)addCar:(Car *)car toPerson:(Person *)person
{
    [_db open];
    
    //根据person是否拥有car来添加car_id
    NSNumber *maxID = @(0);
    
    FMResultSet *res = [_db executeQuery:[NSString stringWithFormat:@"select * from car where own_id = %@",person.ID]];
    while ([res next]) {
        if ([maxID integerValue]<[[res stringForColumn:@"car_id"] integerValue]) {
            maxID = @([[res stringForColumn:@"car_id"] integerValue]);
        }
    }
    
    maxID = @([maxID integerValue] + 1);
    [_db executeUpdate:@"insert into car(own_id,car_id,car_brand,car_price) value(?,?,?,?)",person.ID,maxID,car.brand,@(car.price)];
    
    
    [_db close];
}


//给person删除车辆
-(void)deleteCar:(Car *)car fromPerson:(Person *)person
{
    [_db open];
    [_db executeUpdate:@"delete from car where own_id = ? and car_id = ?",person.ID,car.car_id];
    [_db close];
}

//获取person所有车辆
-(NSMutableArray *)getAllCarsFromPerson:(Person *)person
{
    [_db open];
    NSMutableArray *carArray = [NSMutableArray array];
    FMResultSet *res = [_db executeQuery:[NSString stringWithFormat:@"select * from car where own_id = %@",person.ID]];
    while ([res next]) {
        Car *car = [Car new];
        car.own_id = person.ID;
        car.car_id = @([[res stringForColumn:@"car_id"] integerValue]);
        car.brand = [res stringForColumn:@"car_brand"];
        car.price =  [[res stringForColumn:@"car_price"] integerValue];
        [carArray addObject:car];
    }
    [_db close];
    
    return carArray;
}


//删除person所有车辆
-(void)deleteAllCarsFrom:(Person *)person
{
    [_db open];
    
    [_db executeUpdate:@"delete from car where own_id = ?",person.ID];
    [_db close];
}

@end
